# @shuaige : 陈世玉
# @name :province_fenxi.py
# @time :2024/12/6 20:44
from pyecharts.charts import Map
from pyecharts.options import *
with open('data1.txt', 'r', encoding='utf-8') as file:
    lines = file.readlines()
def province_analysis(lines):
    province_count = {}
    # 解析每一行数据并统计省份数量
    for line in lines[0:]: #从第一行开始
        parts = line.split()
        province = parts[2]# 第三个字段是省份
        #对省份数据进行分析
        if province=='上海' or province=='北京' or province=='天津' or province=='重庆':
            province+='市'
        elif province=='广西':
            province+='壮族自治区'
        elif province=='宁夏':
            province+='回族自治区'
        elif province=='新疆':
            province+='维吾尔自治区'
        elif province=='西藏':
            province+='自治区'
        elif province=='内蒙古':
            province+='自治区'
        else :
            province+='省'
        if province in province_count:
            province_count[province] += 1
        else:
            province_count[province] = 1

    # 提取省份和数量
    return province_count

data_list=list(province_analysis(lines).items())
print(data_list)
map =Map()#创建地图对象
map.add("该地区大学数量",data_list,"china")
map.set_global_opts(
    title_opts=TitleOpts(title="前五百大学全国分布地图"),
    visualmap_opts=VisualMapOpts(
        is_show=True,#是否显示
        is_piecewise=True,#是否分段
        pieces=[
            {"min":1,"max":5 ,"label":"1~5","color":"#CCFFFF"},
            {"min":5,"max":10 ,"label":"5~10","color":"#FFFF99"},
            {"min":10,"max":20 ,"label":"10~20","color":"#ffd299"},
            {"min":20,"max":30 ,"label":"20~30","color":"#ff99a1"},
            {"min":30,"max":50 ,"label":"30~50","color":"#e52443"}
        ]


    )

)
map.render("前五百中国大学分布图.html")

